/**
 * 载入项目引导文件
 */
require('../bootstrap');

/**
 * 载入树结构select的option的html
 * @param secId
 */
let loadTreeOption = (secId) => {
    utils.ajaxGet(page_params.treeoption, {'secId': secId, 'listType': 'all'},
        (response, data) => {
            $("#box-form select[name='parent_id']").html(response.data);
        });
}

let box;
let asxBox;
let nowNode;

//初始化树结构
let initTree = () => {
    let tree_data = {};
    utils.ajaxGet(page_params.url_list, {}, (response, data) => {
        if (response.data.treeList) {
            tree_data = response.data.treeList;
            $('#tree').treeview({data: tree_data, levels: 99, showTags: !0, collapseIcon: 'fa fa-chevron-down', expandIcon: 'fa fa-chevron-right'});
            //绑定选中事件
            $('#tree').on('nodeSelected', function (event, data) {
                asxBox = layer.open({
                    type: 1,
                    title: '角色操作',
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    skin: 'layui-layer-molv', //加上边框
                    area: ['520px', '150px'], //宽高
                    content: $('#box_ask')
                });
                nowNode = data;
            });
        }
    });
};


$(document).ready(() => {
    //切换树结构
    initTree();
    //搜索重新刷新菜单
    $("#btn-search").on('click', function () {
        initTree();
    });
    //添加顶级角色
    $("#btn-add").on('click', () => {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_add);
        $("#box-form input[name='role_id']").val(0);
        $("#box-form input[name='parent_id']").val(0);
        box = layer.open({
            type: 1,
            title: '添加【顶级角色】',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '480px'], //宽高
            content: $('#box-form')
        });
    });
    //添加子角色
    $("#btn-add-child").on('click', function () {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_add);
        $("#box-form input[name='role_id']").val(0);
        $("#box-form input[name='parent_id']").val(nowNode.role_id);
        box = layer.open({
            type: 1,
            title: '添加【' + nowNode.display_name + '】子角色',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '480px'], //宽高
            content: $('#box-form')
        });
    });
    //编辑角色
    $("#btn-edit").on('click', function () {
        $("#box-form form").resetForm();
        $('#box-form form').attr('action', page_params.url_edit);
        $("#box-form input[name='role_id']").val(nowNode.role_id);
        $("#box-form input[name='name']").val(nowNode.name);
        $("#box-form input[name='display_name']").val(nowNode.display_name);
        $("#box-form input[name='parent_id']").val(nowNode.parent_id);
        $("#box-form textarea[name='description']").val(nowNode.description);
        box = layer.open({
            type: 1,
            title: '编辑角色',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '480px'], //宽高
            content: $('#box-form')
        });
    });
    //提交表单
    $('#btn-sub').on('click', function () {
        utils.ajaxSubmitForm($("#box-form form"), '', {}, (response, postdata) => {
            data = response.data;
            console.log(data);
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                layer.close(asxBox);
                initTree($("#group_id").val());
            }
        });
    });
    //删除
    $("#btn-del").on('click', function () {
        let role_id = 0;
        if (nowNode) {
            role_id = nowNode.role_id;
        }
        utils.confirm(page_params.url_del, {'id': role_id}, function (response, postdata) {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(asxBox);
                initTree();
            }
        });
    });
    //编辑权限,参考网址(http://luozhihua.com/jquery-jstree%E9%80%89%E4%B8%AD%E6%88%96%E5%8F%96%E6%B6%88%E9%80%89%E4%B8%AD%E8%8A%82%E7%82%B9.html)
    $('#btn-editperm').on('click', function () {
        $("#box-form-perm form").resetForm();
        $('#box-form-perm form').attr('action', page_params.url_perm);
        $("#box-form-perm input[name='role_id']").val(nowNode.role_id);
        //加载树结构
        $('#jstree').jstree({
            'core': {
                'data': page_params.data_perm
            },
            'types': {
                'default': {
                    'icon': 'fa fa-folder'
                },
            },
            "checkbox": {
                "keep_selected_style": false
            },
            "plugins": ['types', 'checkbox'],
        });
        //取消选中jstree的所有节点
        $("#jstree").jstree('uncheck_all');
        // 批量选中节点
        $("#jstree").jstree('check_node', nowNode.permIds);
        box = layer.open({
            type: 1,
            title: '编辑权限',
            anim: 2,
            shadeClose: true, //开启遮罩关闭
            skin: 'layui-layer-molv', //加上边框
            area: ['620px', '480px'], //宽高
            content: $('#box-form-perm')
        });
    });
    $('#btn-subperm').on('click', function () {
        //已选权限
        var checkedNode = $("#jstree").jstree('get_checked');
        utils.ajaxSubmitForm($("#box-form-perm form"), '', {permIds: checkedNode}, (response, postdata) => {
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success') {
                layer.close(box);
                layer.close(asxBox);
                initTree($("#group_id").val());
            }
        });
    });
});